// pages/duduindex/duduindex.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: 0,
        menu: '',
        menuWidth: 0,
        navigationBar: 0,
        // 拖拽相关数据
        customBoxRight: 0,
        customBoxTop: 150,
        isDragging: false,
        startX: 0,
        startY: 0,
        startRight: 0,
        startTop: 0,
        options:{},
        gridList: [
            {
                img: '/pages/images/two_1.png',
                desc: '班车解决方案',
            },
            {
                img: '/pages/images/two_2.png',
                desc: 'AI线路规划',
            },
            {
                img: '/pages/images/two_3.png',
                desc: '成功案例',
            },
        ],
        advantageList: [
            { img: '/pages/images/system.png' },
            { img: '/pages/images/price.png' },
            { img: '/pages/images/service.png' },
        ],
        descList: [
            { num: '1500', desc: '服务企业' },
            { num: '10000', desc: '运营线路' },
            { num: '98', desc: '客户满意度' },
        ],
        logoList: [
            { logo: '/pages/images/logo_1.png' },
            { logo: '/pages/images/logo_2.png' },
            { logo: '/pages/images/logo_3.png' },
            { logo: '/pages/images/logo_4.png' },
            { logo: '/pages/images/logo_5.png' },
            { logo: '/pages/images/logo_6.png' },
            { logo: '/pages/images/logo_7.png' },
            { logo: '/pages/images/logo_8.png' },
            { logo: '/pages/images/logo_9.png' },
            { logo: '/pages/images/logo_10.png' },
            { logo: '/pages/images/logo_11.png' },
            { logo: '/pages/images/logo_12.png' },
            { logo: '/pages/images/logo_13.png' },
            { logo: '/pages/images/logo_14.png' },
            { logo: '/pages/images/logo_15.png' },
            { logo: '/pages/images/logo_16.png' },
        ],
        isVisble:false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      
      console.log();
        console.log('onLoad', wx.getSystemInfoSync());
        this.setData({
            options: options,
            isVisble: options?.isVisble ?? false,
            menu: wx.getMenuButtonBoundingClientRect(),
            statusBarHeight: wx.getSystemInfoSync()?.statusBarHeight || 0,
            navigationBar: (wx.getMenuButtonBoundingClientRect().top - wx.getSystemInfoSync()?.statusBarHeight) * 2 + wx.getMenuButtonBoundingClientRect().height,
            menuWidth: wx.getMenuButtonBoundingClientRect().width + 10
        })
        console.log('menu', this.data.menu);
        console.log('statusBarHeight', this.data.statusBarHeight);
        console.log('navigationBar', this.data.navigationBar);
    },
    goback(){
      wx.navigateBack()
    },
    onChange(event) {
        this.setData({
            value: event.detail
        })
    },
    moreClick() {
        wx.navigateTo({
            url: '/pages/duduwebview/duduwebview?link=1'
        })
    },
    moreCatch() {
        wx.navigateTo({
            url: '/pages/duduwebview/duduwebview?link=2'
        })
    },
    LookPartner() {
        wx.navigateTo({
            url: '/pages/duduwebview/duduwebview?link=3'
        })
    },
    goCar() {
        wx.navigateTo({
            url: '/pages/duduwebview/duduwebview?link=4'
        })
    },
    customClick() {
        console.log('点击客服按钮');

        // 检查微信版本
        const systemInfo = wx.getSystemInfoSync();
        console.log('微信版本:', systemInfo.version);
        console.log('微信基础库版本:', systemInfo.SDKVersion);

        // 使用微信原生客服功能
        wx.openCustomerServiceChat({
            extInfo: {
                url: 'https://work.weixin.qq.com/kfid/kfcf259c4dbefc39c35'
            },
            corpId: 'ww687605fb03b47862',
            success: function (res) {
                console.log('客服聊天打开成功', res);
            },
            fail: function (err) {
                console.log('客服聊天打开失败', err);
                console.log('错误详情:', JSON.stringify(err));
            }
        });
    },

    // 拖拽开始
    onTouchStart: function (e) {
        if (e.touches.length !== 1) return;

        var touch = e.touches[0];
        this.setData({
            isDragging: true,
            startX: touch.clientX,
            startY: touch.clientY,
            startRight: this.data.customBoxRight,
            startTop: this.data.customBoxTop
        });
    },

    // 拖拽移动
    onTouchMove: function (e) {
        if (!this.data.isDragging || e.touches.length !== 1) return;

        var touch = e.touches[0];
        var deltaX = touch.clientX - this.data.startX;
        var deltaY = touch.clientY - this.data.startY;

        // 获取系统信息
        var systemInfo = wx.getSystemInfoSync();
        var windowWidth = systemInfo.windowWidth;
        var windowHeight = systemInfo.windowHeight;

        // 计算新位置（使用right属性）
        var newRight = this.data.startRight - deltaX; // right值减少表示向右移动
        var newY = this.data.startTop + deltaY;

        // 限制X轴范围（right值范围：0 到 windowWidth-200）
        var maxRight = windowWidth - 200; // 屏幕宽度 - 盒子宽度
        newRight = Math.max(0, Math.min(newRight, maxRight));

        // 限制Y轴范围
        var minY = this.data.statusBarHeight + this.data.navigationBar;
        var maxY = windowHeight - 200;
        newY = Math.max(minY, Math.min(newY, maxY));

        // 实时更新位置
        this.setData({
            customBoxRight: newRight,
            customBoxTop: newY
        });
    },

    // 拖拽结束
    onTouchEnd: function (e) {
        if (!this.data.isDragging) return;

        this.setData({
            isDragging: false
        });

        // 松手后自动吸附到右边
        this.animateToRight();
    },
    // 动画吸附到右边
    animateToRight: function () {
        // 直接设置为右边（right: 0）
        this.setData({
            customBoxRight: 0
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})